﻿<%@ WebHandler Language="C#" Class="BorrowHandlerService" %>

using System;
using System.Web;



using System.Collections;
using System.Collections.Generic;
using System.Reflection;

using XJX.BusinessLayer;

using XJX.Page;

public class BorrowHandlerService : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "application/json";

        string response = "";

        String methodName = context.Request["method"];
        Type type = this.GetType();
        MethodInfo method = type.GetMethod(methodName);


        if (method == null)
        {
            response = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "调用的方法不存在！").ToJson(); return;
        }

        try
        {
            BeforeInvoke(methodName);
            response = method.Invoke(this, null).ToString();
        }
        catch (Exception ex)
        {
            Hashtable result = new Hashtable();
            result["error"] = -1;
            result["message"] = ex.Message;
            result["stackTrace"] = ex.StackTrace;
            String json = XJX.BusinessLayer.JSON.Encode(result);
            context.Response.Clear();
            context.Response.Write(json);
        }
        finally
        {
            AfterInvoke(methodName);
            HttpContext.Current.Response.Write(response);
        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    //权限管理
    protected void BeforeInvoke(String methodName)
    {
        //Hashtable user = GetUser();
        //if (user.role == "admin" && methodName == "remove") throw .      
    }
    //日志管理
    protected void AfterInvoke(String methodName)
    {

    }


    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================
    //===============================================================================================================================================


    //借书动作
    public string Add_New_Borrow()
    {
        string bnum = ((ParamInfo)HttpContext.Current.Session["ParamInfo"]).Bnum;
        
        FamilyInfo fi = (FamilyInfo)HttpContext.Current.Session["FamilyInfo"];
        string JUID = fi.UID;//对应是借书人的UID
        string CUID = HttpContext.Current.Request["BUID"];//对应图书的所有者UID，即图书对应的家庭编号。
        string BID = HttpContext.Current.Request["BID"];

        int ReturnValue = 0;

        //下面开始调用信息保存
        bool b = XJX.Page.Borrow.Add_New_Borrow(JUID, CUID, BID, out ReturnValue);
        
        string result = "";
        switch (ReturnValue)
        {
            case 0:
                //下面调用两个短信发送动作
                int i=XJX.Page.Helper.CallTemplater(1, "toborrowtitle", "toborrowtemplate", JUID, BID, "",System.DateTime.Now);//发给借阅人的短信。
                int j=XJX.Page.Helper.CallTemplater(1, "toholdertitle", "toholdertemplate", CUID, BID, fi.Mobile,System.DateTime.Now);//发送给图书所有人的短信。


                if (i + j == 2)
                {
                    //前台显示借阅成功信息。
                    result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "借阅成功！").ToJson();
                }
                else
                {
                    result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Success, "借书动作完成，但短信发送失败！").ToJson();
                }
                break;
            case 1:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "抱歉，图书已经有人借出！").ToJson();
                break;
            case 2:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "抱歉，图书已经借出，尚未归还！").ToJson();
                break;
            case 3:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "图书当前已经过期，正在督促归还中！").ToJson();
                break;
            case 4:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "借阅此图书的前次流程尚未到前额确认结束，等待管理员处理后再借！").ToJson();
                break;
                
            case 98:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "抱歉，您已经借满"+bnum+"本，不能再借！").ToJson();
                break;
            case 99:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "借阅中发生错误，请稍后再借!").ToJson();
                break;
            case 100:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Error, "您的家庭上传图书数未达标，暂不能借书!").ToJson();
                break;
            case 101:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "抱歉，下架图书不能借阅!").ToJson();
                break;
            case 102:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "抱歉，拥有该图书的家庭已经不支持借出!").ToJson();
                break;
            case 200:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "对不起，暂未开放图书借阅功能，请稍后……!").ToJson();
                break;
           
            default:
                result = new XJX.Page.JsonResult(XJX.Page.StatusMessageType.Hint, "抱歉，当前错误暂时不能处理!").ToJson();
                break;                
        }
        return result;
    }

    //获得图书相关信息
    public string Get_Family_Borrow_Book_List()
    {
        int PageIndex = Convert.ToInt32(HttpContext.Current.Request["PageIndex"]);
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["PageSize"]);
        int ProcedureID = int.Parse(HttpContext.Current.Request["ProcedureID"]);

        string UID = ((FamilyInfo)HttpContext.Current.Session["FamilyInfo"]).UID;

        string procname = "";

        switch (ProcedureID)
        {
            case 1://得到正借来的图书（包括未确认和已经确认[旧方法]）
                procname = "Get_Borrowing_In_Book_List";
                break;
            case 2://得到正借出的图书(包括未确认和已经确认[旧方法])
                procname = "Get_Borrowing_Out_Book_List";
                break;
            case 3://得到曾经借来的图书
                procname = "Get_Borrowed_In_Book_List";
                break;
            case 4://得到曾经借出的图书
                procname = "Get_Borrowed_Out_Book_List";
                break;

            case 5://得到正借入的、尚未确认的图书
                procname = "Get_Family_Borrowing_In_Book_Unconfirm_List";
                break;
            case 6://得到正借入的、已经确认的图书
                procname = "Get_Family_Borrowing_In_Book_Confirm_List";
                break;
            case 7://得到正借出的、尚未确认的图书
                procname = "Get_Family_Borrowing_Out_Book_Unconfirm_List";
                break;
            case 8://得到正借出的、已经确认的图书
                procname = "Get_Family_Borrowing_Out_Book_Confirm_List";
                break;
        }

        return XJX.Page.Borrow.Get_Borrow_Book_List(procname, UID, PageIndex, PageSize);
    }
}